﻿using System;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

namespace WinnerCMS.Common
{
    /// <summary>
    /// 数据验证
    /// </summary>
    public class MyValidate
    {
        #region 验证数据的正则表达式
        /// <summary>
        /// 检察是否都是数字 
        /// </summary>
        /// <param name="inPut">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsNumeric(string inPut)
        {
            return IsNumeric(inPut, false, false);
        }
        /// <summary>
        /// 检察是否都是数字,支持负数和小数
        /// </summary>
        /// <param name="inPut"></param>
        /// <param name="allowNegative"></param>
        /// <param name="allowDecimal"></param>
        /// <returns></returns>
        public static bool IsNumeric(string inPut, bool allowNegative, bool allowDecimal)
        {

            return Information.IsNumeric(inPut);

        }

        /// <summary>
        /// 检察是否正确的Email格式
        /// </summary>
        /// <param name="inPut">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsEmail(string inPut)
        {
            return RegexHepler.EMail.IsMatch(inPut);
        }

        /// <summary>
        /// 检察是否正确的日期格式
        /// </summary>
        /// <param name="inPut">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsDate(string inPut)
        {
            //考虑到了4年一度的366天，还有特殊的2月的日期
            return RegexHepler.Date.IsMatch(inPut);
        }

        /// <summary>
        /// 是否是SQL语句
        /// </summary>
        /// <param name="inPut">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsSQL(string inPut)
        {
            Regex reg = new Regex(@"\?|Truncate%20|Truncate\s+|ALTER%20|ALTER\s+|select%20|select\s+|insert%20|insert\s+|delete%20|delete\s+|count\(|drop%20|drop\s+|update%20|update\s+", RegexOptions.IgnoreCase);

            return reg.IsMatch(inPut);
        }
        /// <summary>
        /// 是否为IP
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static bool IsIP(string inPut)
        {
            return Regex.IsMatch(inPut, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
        }

        #endregion

        /// <summary>
        /// 是否为URL
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static bool IsUrl(string inPut)
        {
            ////主要是文章采集用的，匹配比较松。
            //return Regex.IsMatch(InPut, @"^(http|ftp|file)://.*");
            return Regex.IsMatch(inPut, @"^(http|ftp|file)://([\w-]+\.)+[\w-]+(/[\w-\./?%=]*)?$");
        }
    }

}